package com.zsc.wxm.ysh.anjo.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsc.wxm.ysh.anjo.member.cache.MemberRoleCacheService;
import com.zsc.wxm.ysh.anjo.member.cache.RoleCacheService;
import com.zsc.wxm.ysh.anjo.member.mapper.RoleMapper;
import com.zsc.wxm.ysh.anjo.member.model.MemberRole;
import com.zsc.wxm.ysh.anjo.member.model.Role;
import com.zsc.wxm.ysh.anjo.member.service.MemberRoleService;
import com.zsc.wxm.ysh.anjo.member.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author playmaker
 * @since 2021-01-04
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private MemberRoleService memberRoleService;

    @Resource
    private MemberRoleCacheService memberRoleCacheService;

    @Resource
    private RoleCacheService roleCacheService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addRole(String roleName) {
        Role role = new Role();
        role.setName(roleName);
        this.save(role);
        //设置编码，001--->002...
        role.setCode(String.format("%04d", role.getId()));
        this.updateById(role);
        //清除缓存
        roleCacheService.removeRoleListByCache();
    }

    @Override
    public Map<String, Object> findRoleByUserId(Long userId) {
        //获取所以角色
        List<Role> allRoles = roleCacheService.listByCache();

        //根据用户Id 获取用户角色列表
        List<MemberRole> memberRoles = memberRoleCacheService.listByMemberIdCache(userId);

        //收集用户角色id
        List<Long> memberRoleIdList = memberRoles.stream().map(MemberRole::getRoleId).collect(Collectors.toList());

        //对角色进行分类
        List<Role> assignRoles = new ArrayList<>();
        for (Role role : allRoles) {
            //筛选已分别的角色
            if (memberRoleIdList.contains(role.getId())) {
                assignRoles.add(role);
            }
        }

        //返回数据
        Map<String, Object> roleMap = new HashMap<>();
        //已分配给用户的角色
        roleMap.put("assignRoles", assignRoles);
        //所有角色
        roleMap.put("allRoles", allRoles);
        return roleMap;
    }

    @Override
    public void giveMemberToRoles(Long memberId, Long[] roleIds) {
        Assert.notNull(roleIds, "角色Id不能为空");
        //先清除用户的角色
        memberRoleService.remove(new QueryWrapper<MemberRole>().eq("member_id", memberId));
        List<MemberRole> memberRoleList = new ArrayList<>();
        for (Long id : roleIds) {
            if (id == null) {
                continue;
            }
            MemberRole memberRole = new MemberRole();
            memberRole.setRoleId(id);
            memberRole.setMemberId(memberId);
            memberRoleList.add(memberRole);
        }
        //批量插入
        memberRoleService.saveBatch(memberRoleList);
        //清除缓存
        memberRoleCacheService.removeListByMemberIdCache(memberId);
    }
}
